<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Task;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TaskController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Task(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('task_type')
                 ->using([1 => '每日任务',2 => '成长任务'])
                 ->label([1 => 'success',2 => 'danger']);
            $grid->column('name');
            $grid->column('give_points');
            $grid->column('status')
                ->using([1 => '启用',2 => '禁用'])
                ->label([1 => 'success',2 => 'danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->panel();
                $filter->like('name')->width(3);
                $filter->equal('task_type')
                       ->select([1 => '每日任务',2 => '成长任务'])
                        ->width(3);
                $filter->equal('status')
                       ->select([1 => '启用',2 => '禁用'])
                      ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Task(), function (Show $show) {
            $show->field('id');
            $show->field('task_type');
            $show->field('name');
            $show->field('give_points');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Task(), function (Form $form) {
            $form->display('id');
            $form->radio('task_type')
                 ->options([1 => '每日任务',2 => '成长任务'])
                ->default(2)
                 ->required();
            $form->text('name')
                 ->maxLength(120)
                 ->required();
            $form->number('give_points')
                 ->min(0)
                 ->required();
            $form->radio('status')
                 ->options([1 => '启用',2 => '禁用'])
                 ->default(1)
                 ->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
